<?php

try
{
	require_once 'init.inc';
	require_once 'cf/mail.inc';
	require 'PHPMailer/PHPMailerAutoload.php';

	$page = new Page(12);
	$title = $page->title();
	$keywordsOb = new KeyWords('cf_pages', 'id', $page->id());
	$keywords = $keywordsOb->words();
	
	if (array_key_exists('name',$_REQUEST)){
		$msg = 
			"Наименование организации: ".$_REQUEST['name']."\n".
			"Контактное лицо: ".$_REQUEST['person']."\n".
			"Телефон: ".$_REQUEST['phone']."\n";
		
		if (array_key_exists('email',$_REQUEST)){
			$msg .= "E-mail: " . $_REQUEST['email']."\n";
		}
		if (array_key_exists('site',$_REQUEST)){
			$msg .= "Сайт магазина: " . $_REQUEST['site']."\n";
		}
		if (array_key_exists('address',$_REQUEST)){
			$msg .= "Фактический адрес: " . $_REQUEST['address']."\n";
		}
		if (array_key_exists('details',$_REQUEST)){
			$msg .= "Реквизиты компании: " . $_REQUEST['details']."\n";
		}

		$mail = new PHPMailer;
		$mail->CharSet = 'utf-8';
		$mail->FromName = 'LODKI-PALATKI.RU';
		$mail->addAddress('lodkipalatki@yandex.ru');
		$mail->addBCC('max.seadesign@gmail.com');
		$mail->Subject = "Пишет оптовик с сайта lodki-palatki.ru";
		$mail->Body = $msg;
		if (array_key_exists('details_file', $_FILES) && $_FILES['details_file']['error']==UPLOAD_ERR_OK && $_FILES['details_file']['size']>0) {
			$tmpNm = tempnam(sys_get_temp_dir(),'details_');
			if (move_uploaded_file($_FILES['details_file']['tmp_name'], $tmpNm)) {
				$mail->addAttachment($tmpNm, $_FILES['details_file']['name']);
			}
		}
		if(!$mail->send()) {
		   echo 'Message could not be sent.';
		   echo 'Mailer Error: ' . $mail->ErrorInfo;
		   exit;
		}
		
		Header('Location:/page.php?id=12&sent=1');
	}
	
	$sent = array_key_exists('sent',$_REQUEST);
	
	assignTemplateVars('page','title','keywords','sent');
	display('opt');
}
catch (Exception $e)
{
	header('Location: error.php');
}

?>